package cn.cdeden.aiqa.controller;

import java.util.List;

import cn.cdeden.aiqa.domain.bo.ChatGptHelperdQueryParam;
import cn.cdeden.aiqa.domain.bo.ChatgptQuestionUserDto;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cdeden.common.idempotent.annotation.RepeatSubmit;
import cn.cdeden.common.log.annotation.Log;
import cn.cdeden.common.web.core.BaseController;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.validate.AddGroup;
import cn.cdeden.common.core.validate.EditGroup;
import cn.cdeden.common.log.enums.BusinessType;
import cn.cdeden.common.excel.utils.ExcelUtil;
import cn.cdeden.aiqa.domain.vo.WikiHelperVo;
import cn.cdeden.aiqa.domain.bo.WikiHelperBo;
import cn.cdeden.aiqa.service.IWikiHelperService;
import cn.cdeden.common.core.utils.page.TableDataInfo;

/**
 * 智能体管理
 * 前端访问路由地址为:/aiqa/helper
 *
 * @author Nick
 * @date 2025-02-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/helper")
public class WikiHelperController extends BaseController {

    private final IWikiHelperService wikiHelperService;


    @GetMapping(value = "/get")
    @Log(title = "智能体管理：分页查询列表", businessType = BusinessType.OTHER)
    public TableDataInfo<WikiHelperVo> query(ChatGptHelperdQueryParam query, Pageable pageable) {
        return wikiHelperService.select(query, pageable);
    }

    /**
     * 智能体管理：查询所有（基于用户）
     * @param query
     * @return
     */
    @GetMapping(value = "/getAll")
    @Log(title = "智能体管理：查询所有（基于用户）", businessType = BusinessType.OTHER)
    public R<List<WikiHelperVo>> query(ChatGptHelperdQueryParam query) {
        return wikiHelperService.selectAll(query);
    }

    /**
     * 查询智能体管理列表
     */
    @SaCheckPermission("aiqa:helper:list")
    @GetMapping("/list")
    public TableDataInfo<WikiHelperVo> list(WikiHelperBo bo, PageQuery pageQuery) {
        return wikiHelperService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出智能体管理列表
     */
    @SaCheckPermission("aiqa:helper:export")
    @Log(title = "智能体管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WikiHelperBo bo, HttpServletResponse response) {
        List<WikiHelperVo> list = wikiHelperService.queryList(bo);
        ExcelUtil.exportExcel(list, "智能体管理", WikiHelperVo.class, response);
    }

    /**
     * 获取智能体管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("aiqa:helper:query")
    @GetMapping("/{id}")
    public R<WikiHelperVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wikiHelperService.queryById(id));
    }

    /**
     * 新增智能体管理
     */
    @SaCheckPermission("aiqa:helper:add")
    @Log(title = "智能体管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WikiHelperBo bo) {
        return toAjax(wikiHelperService.insertByBo(bo));
    }

    /**
     * 修改智能体管理
     */
    @SaCheckPermission("aiqa:helper:edit")
    @Log(title = "智能体管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WikiHelperBo bo) {
        return toAjax(wikiHelperService.updateByBo(bo));
    }

    /**
     * 删除智能体管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("aiqa:helper:remove")
    @Log(title = "智能体管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wikiHelperService.deleteWithValidByIds(List.of(ids), true));
    }

    @PostMapping(value = "/addHelper")
    @Log(title = "智能体管理：给用户添加助手", businessType = BusinessType.OTHER)
    public R<Void> addHelper(@RequestBody ChatgptQuestionUserDto questionUserDto) {
        wikiHelperService.addHelper(questionUserDto);
        return R.ok();
    }

    @PostMapping(value = "/delHelper")
    @Log(title = "智能体管理：去除用户的助手", businessType = BusinessType.OTHER)
    public R<Void> delHelper(@RequestBody ChatgptQuestionUserDto questionUserDto) {
        wikiHelperService.delHelper(questionUserDto);
        return R.ok();
    }
}
